import os
import json
import numpy as np
import cv2

color = {'green':(0,255,0),
        'blue':(255,165,0),
        'dark red':(0,0,139),
        'red':(0, 0, 255),
        'dark slate blue':(139,61,72),
        'aqua':(255,255,0),
        'brown':(42,42,165),
        'deep pink':(147,20,255),
        'fuchisia':(255,0,255),
        'yello':(0,238,238),
        'orange':(0,165,255),
        'saddle brown':(19,69,139),
        'black':(0,0,0),
        'white':(255,255,255)}

def draw_boxes(img, boxes, line_color='green'):
    img = img.copy()
    width = img.shape[1]
    height = img.shape[0]
    line_color = color[line_color]

    text_scale = max(1, img.shape[1] / 800.)
    text_thickness = 1 if text_scale > 1.1 else 1
    text_offset = int(16*text_scale)
    line_thickness = max(1, int(img.shape[1] / 800.))

    for box in boxes:
        one_box = box
        one_box = np.array([max(one_box[0], 0), max(one_box[1], 0),
                    min(one_box[2], width), min(one_box[3], height)])
        x1,y1,x2,y2 = np.array(one_box[:4]).astype(int)
        cv2.rectangle(img, (x1,y1), (x2,y2), line_color, line_thickness)
        text_str = '{:.02f}'.format(box[4])
        cv2.putText(img, text_str, (x1, y1 + text_offset), cv2.FONT_HERSHEY_PLAIN, text_scale, (0, 0, 255),
                    thickness=text_thickness)
    return img
